//储存正在聊天好友的信息

import { ref } from 'vue'
import { format } from 'date-fns'
import { opt } from './user'
import axios from 'axios'

export const friendId = ref('')
export const friendName = ref('')
export const type = ref('')
export const friendHeadShot = ref('')
export const friendGender = ref('')
export const friendBirthday = ref('')
export const friendPhone = ref('')
export const masterId = ref('')

import img from '@/assets/1.jpg'

export function setFriendInfo() {
    const friend = JSON.parse(sessionStorage.getItem('friend'))
    friendId.value = friend.id
    friendName.value = friend.name
    if (friend.birthday !== null) {
        friendBirthday.value = format(friend.birthday, 'yyyy-MM-dd')
    }
    else {
        friendBirthday.value = ''
    }
    if (friend.type === 'user') {
        friendPhone.value = friend.phone
        friendGender.value = friend.gender
    }
    type.value = friend.type
    if(opt.value === 0)
        friendHeadShot.value = "https://47.120.69.214:8787/" + friend.headShotUrl
    else
        friendHeadShot.value = img
    if(type.value === 'group'){
        masterId.value = friend.masterId
    }
}


export function deleteFriend(userId, friendId){
    sessionStorage.removeItem('friend')
    axios({
        menthod: 'get',
        url : 'https://47.120.69.214:8787/friend/deleteFriend',
        params:{
            "userId1" : userId,
            "userId2" : friendId
            }
    })
    .then( response => {})
    .catch( error => console.log(error))
}
